// index.js
const app = getApp()
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return `${year}年${month}月${day}日`
}

// 格式化日期为 yyyy/mm/dd 格式，用于存储
const formatDateForStorage = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return `${year}/${month}/${day}`
}

Page({
  data: {
    motto: 'Hello World',
    hasUserInfo: false,
    canIUseGetUserProfile: false,
    canIUseNicknameComp: true,
    
    // 奶茶记录相关数据
    selectedDate: formatDate(new Date()),
    teaSpots: [], // 有奶茶记录的日期
    dailyTeaRecords: [], // 当日的奶茶记录
    totalRecords: 0,
    thisMonthRecords: 0,
    favoriteShop: '无'
  },
  
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  
  onShow() {
    // 每次页面显示时加载数据
    this.loadTeaRecords();
  },
  
  // 分享给好友
  onShareAppMessage() {
    return {
      title: `我已记录了${this.data.totalRecords}杯奶茶，最爱的是${this.data.favoriteShop}！`,
      path: '/pages/index/index',
    };
  },

  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: `我的奶茶记录：本月已喝${this.data.thisMonthRecords}杯，最爱${this.data.favoriteShop}！`,
      query: 'from=timeline',
    };
  },
  
  // 加载奶茶记录数据
  loadTeaRecords() {
    // 从本地存储中获取记录
    const teaRecords = wx.getStorageSync('teaRecords') || [];
    const statistics = wx.getStorageSync('teaStatistics') || {
      totalRecords: 0,
      thisMonthRecords: 0,
      favoriteShop: '无'
    };
    
    // 提取所有有记录的日期
    const spots = [];
    teaRecords.forEach(record => {
      if (record.date && !spots.includes(record.date)) {
        spots.push(record.date);
      }
    });
    
    // 获取当前日期的记录
    const today = formatDateForStorage(new Date());
    const todayRecords = teaRecords.filter(record => record.date === today);
    
    this.setData({
      teaSpots: spots,
      dailyTeaRecords: todayRecords,
      totalRecords: statistics.totalRecords,
      thisMonthRecords: statistics.thisMonthRecords,
      favoriteShop: statistics.favoriteShop
    });
  },
  
  // 点击日历日期
  onDayClick(e) {
    const date = e.detail.date;
    
    // 从本地存储中获取记录
    const teaRecords = wx.getStorageSync('teaRecords') || [];
    
    // 筛选选中日期的记录
    const dayRecords = teaRecords.filter(record => record.date === date);
    
    // 格式化日期显示
    const dateParts = date.split('/');
    const formattedDate = `${dateParts[0]}年${dateParts[1]}月${dateParts[2]}日`;
    
    this.setData({
      selectedDate: formattedDate,
      dailyTeaRecords: dayRecords
    });
  },
  
  // 添加新奶茶记录
  addTeaRecord() {
    wx.navigateTo({
      url: '/pages/addTea/addTea',
    })
  },
  
  // 以下为用户信息相关方法
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  
  getUserProfile(e) {
    wx.getUserProfile({
      desc: '展示用户信息',
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    this.setData({
      'userInfo.avatarUrl': avatarUrl,
    })
  },
  
  onInputChange(e) {
    const nickName = e.detail.value
    this.setData({
      'userInfo.nickName': nickName,
      hasUserInfo: true
    })
  },
  
  // 在 index.js 中添加进入统计页面的方法
  goToStatistics() {
    wx.navigateTo({
      url: '/pages/statistics/statistics',
    })
  },
  
  // 添加进入喝奶茶页面的方法
  goToDrinkTea() {
    wx.navigateTo({
      url: '/pages/drinkTea/drinkTea',
    })
  }
})
